//
//  STHomeViewController.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/26.
//

import UIKit

let User_touxiang_path_key = "User_touxiang_local_path_key"

class STHomeViewController: GPBaseViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
 {
    
    let requestApi = GPRequestApi.init()
    var userInfo:  Dictionary<String, Any> = [:]
    var waitOrderNum: Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        setNavBarTitle("海铁货运")
        addSubviewForPage()
        
        requestApi.getUserInfo() {
            (isSuccess, response, error)  in
            if(isSuccess) {
                let responseData = response as! Dictionary<String, Any>
                let data = responseData["data"] as! Dictionary<String, Any>
                self.userInfo = data
                self.headView.updateValue(data)
            }
        }
        requestWaitOrderList()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        requestWaitOrderList()
    }

    func addSubviewForPage() {
        addTopBGView()
        
        view.addSubview(headView)
        headView.snp.makeConstraints { (make) in
            make.top.equalTo(isShortScreen ? 33 : 43)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(70)
        }
        
        view.addSubview(bottomView)
        bottomView.snp.makeConstraints { (make) in
            make.top.equalTo(headView.snp.bottom).offset(isShortScreen ? 25 : 35)
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }
        
        let msgView = UIImageView.init(image: UIImage.init(named: "tixing"))
        bottomView.addSubview(msgView)
        msgView.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
        
        bottomView.addSubview(msgLabel)
        msgLabel.snp.makeConstraints { (make) in
            make.left.equalTo(56)
            make.centerY.equalTo(bottomView)
            make.right.equalTo(-6)
            make.height.equalTo(22)
        }
        
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(bottomView.snp.bottom).offset(10)
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.bottom.equalTo(0)
        }
        
        STHomeBoxCollectionViewCell.registerTo(collectionView)
        headView.addTargetForIcon(self, action: #selector(handleIconAction))
    }
    
    func requestWaitOrderList () {
        requestApi.getCurrentWaitOrder() {
            (isSuccess, response, error)  in
            if(isSuccess) {
                let responseData = response as! Dictionary<String, Any>
                let data = responseData["data"] as! Array<Dictionary<String, Any>>
                self.waitOrderNum = data.count
                if(data.count > 0) {
                    self.msgLabel.text = "您有 \(data.count) 笔新订单正在运行！"
                } else {
                    self.msgLabel.text = "您暂时没有新订单需要处理"
                }
            }
        }
    }
    
    // UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let h = 189 * (kScreenWidth - 64) / 320
        return CGSize.init(width: (kScreenWidth - 64) / 2.0, height: h)
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = STHomeBoxCollectionViewCell.reusableCell(collectionView, indexPath: indexPath)
        cell?.updateCellData(boxType: indexPath.item)
        return cell ?? collectionView.dequeueReusableCell(withReuseIdentifier: STHomeBoxCollectionViewCell.identifier, for: indexPath)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch indexPath.item {
            case 0:
                if(waitOrderNum > 0) {
                    let newVC: STOrderNewViewController = STOrderNewViewController.init()
                    navigationController?.pushViewController(newVC, animated: true)
                } else {
                    self.toastInfo("暂时没有新订单需要处理")
                }
                break
            case 1:
                let historyVC: STOrderHistoryViewController = STOrderHistoryViewController.init()
                navigationController?.pushViewController(historyVC, animated: true)
                break
            case 2:
                let guidanceVC: STGuidanceViewController = STGuidanceViewController.init()
                navigationController?.pushViewController(guidanceVC, animated: true)
                break
            case 3:
                let setVC: STSettingViewController = STSettingViewController.init()
                navigationController?.pushViewController(setVC, animated: true)
                break
            default: break
        }
    }
    
    @objc func handleIconAction () {
        let personalVC: STPersonalViewController = STPersonalViewController.init()
        navigationController?.pushViewController(personalVC, animated: true)
    }
    
    lazy var headView: STUserInfoView = {
        let userView = STUserInfoView.init(frame: CGRect.zero)
        return userView
    }()
    
    lazy var bottomView: UIView = {
        let top_Height = relativeHeight(w: kScreenWidth - 32, h: 205)
        let v = UIView.init(frame: CGRect.init(x: 16, y: CGFloat(top_Height), width: kScreenWidth - 32 , height: 55))
        return v
    }()

    lazy var msgLabel: UILabel = {
        let lb = UILabel.init()
        lb.textColor = UIColor.init(hexString: "#323232", alpha: 1)
        lb.text = "您暂时没有新订单需要处理"
        return lb
    }()
    
    lazy var collectionView: UICollectionView = {
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
        layout.minimumLineSpacing = 20
        layout.minimumInteritemSpacing = 16
        let collectView: UICollectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        collectView.delegate = self
        collectView.dataSource = self
        collectView.backgroundColor = UIColor.init(hexString: "#F7F7F7", alpha: 1)
        return collectView
    }()
}
